//
//  SKUDefultGraphV.m
//  ZITHER
//
//  Created by weixin on 2018/8/16.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "SKUDefultGraphV.h"

@implementation SKUDefultGraphV

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.userInteractionEnabled = YES;
        
        [self setLoad];
        
        [self setUI];
        
    }
    return self;
}


-(void)setModel:(skuDetailModel *)model{
    
    _model = model;

    
    
    
    if ([model.myEnquiryFlag isEqualToString:@"1"]) {
        self.btn1.hidden = YES;
      
    }else{
        self.btn1.hidden = NO;
    }
        if (self.model.conjuncturePrice.length == 0) {
            self.labOfficial.hidden = YES;
            self.imageOfficial.hidden = YES;
            self.labSubOfficial.hidden = YES;
        }else{
            self.labOfficial.hidden = NO;
            self.imageOfficial.hidden = NO;
            self.labSubOfficial.hidden = NO;
            
            /* TODO: ================= 做个价格对比显示图标 =================*/
            self.imageOfficial.image = [UIImage imageNamed:@"sku_icon_fall"];
            self.labSubOfficial.text = @"公价";
            self.labOfficial.text = self.model.conjuncturePrice;
        }
        

        
        if (self.model.price.length == 0) {
            self.imageVessel.hidden = YES;
            self.labVessel.hidden = YES;
            self.labSubVessel.hidden = YES;
        }else{
            self.imageVessel.hidden = NO;
            self.labVessel.hidden = NO;
            self.labSubVessel.hidden = NO;
            
            self.imageVessel.image = [UIImage imageNamed:@"sku_icon_rise"];
            self.labSubVessel.text = @"二手价";
            self.labVessel.text = self.model.price; //二手价
        }
    
}

-(void)setUI{

    [self setGV];

    self.btn1 = [[QMUIButton alloc]initWithFrame:CGRectMake(30,430,kScreenW-60,50)];
    [self.btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btn1 setTitle:@"开启我的询价" forState:UIControlStateNormal];
    [self.btn1 setTitle:@"重启我的询价" forState:UIControlStateSelected];
    @weakify(self);
    [[self.btn1 rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (!kUSER_INFO_MANAGER.userIsLogin) {
            [Tools pushSinge];            
            return ;
        }
        [[serviceDefualt loadopenEnquiry:self.model.goodsId]subscribeNext:^(ResponseData* x) {
            if (x.success) {
                [kUSER_INFO_MANAGER MyList];
                if (self.btn1.selected) {
                    [Tools showTips:@"重启询价成功"];
                    if (self.blockUpLoadData) {
                        self.blockUpLoadData();
                    }
                    [self blockUpLoadData];
                }else{
                    self.btn1.selected = !self.btn1.selected;
                    [Tools showTips:@"开启询价成功"];
                    if (self.blockUpLoadData) {
                        self.blockUpLoadData();
                    }
                    [self blockUpLoadData];
                }
                
            }else{
                     [Tools showTips:x.errorInfo];
            }
        }];
        
    }];
    
    if ([self.model.enquiryStatus isEqualToString:@"0"]) {
      
        self.hidden = YES;
        
    }else if ([self.model.enquiryStatus isEqualToString:@"1"]){
        
        self.btn1.selected = !self.btn1.selected;

    }else{
        self.hidden = NO;
    }
    
    [self.btn1 setBackgroundColor:[UIColor blackColor]];
    [self addSubview:self.btn1];
    
    [self.btn1 setCornerRadiusWithRadius:12 isMasks:YES];
   
    
}


-(void)setLoad{
    //图标数据源
    self.allValues = @[@"95",@"98",@"92",@"115",@"110",@"112",@"90",@"110",@"107"];
    self.allDates =  @[@"18/01",@"18/02",@"18/03",@"18/04",@"18/05",@"18/06",@"18/07",@"18/08",@"18/09"];
}


-(void)setGV{
   

    YASimpleGraphView *graphView = [[YASimpleGraphView alloc]init];
    graphView.frame = self.frame;
    graphView.backgroundColor = [UIColor whiteColor];
    graphView.allValues = self.allValues;
    graphView.allDates = self.allDates;
    graphView.defaultShowIndex = self.allDates.count-1;
    graphView.delegate = self;
    graphView.lineColor = [UIColor grayColor];
    graphView.lineWidth = 1.0/[UIScreen mainScreen].scale;
    graphView.lineAlpha = 1.0;
    graphView.enableTouchLine = YES;
    [self colorspace:graphView];
    
    graphView.userInteractionEnabled = YES;
    [self addSubview:graphView];
    
    [graphView startDraw];
    
    [RACObserve (self,frame) subscribeNext:^(id x){
        NSValue *value = x;
        CGRect frame =  [value CGRectValue];
        frame.origin.y = 0;
        graphView.frame = frame;
        NSLog(@"");
    }];
}

-(void)colorspace:(YASimpleGraphView*)view{
    //设置渐变
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    
    size_t num_locations = 2;
    
    CGFloat locations[2] = { 0.0, 1.0 };
    
    UIColor *startColor =  baseColorGradientL;
    
    CGFloat *startColorComponents = (CGFloat *)CGColorGetComponents([startColor CGColor]);
    
    UIColor *endColor = baseColorGradientR;
    
    CGFloat *endColorComponents = (CGFloat *)CGColorGetComponents([endColor CGColor]);
    
    CGFloat components[8] = {
        
        startColorComponents[0],
        
        startColorComponents[1],
        
        startColorComponents[2],
        
        startColorComponents[3],
        
        /*起始点的颜色是蓝色*/
        
        endColorComponents[0],
        
        endColorComponents[1],
        
        endColorComponents[2],
        
        endColorComponents[3]
        
        /*终止点的颜色是绿色*/
        
    };
    
    
    //    CGFloat components[8] = {
    //        0.33, 0.67, 0.93, 0.25,
    //        1.0, 1.0, 1.0, 1.0
    //    };
    
    
    //graphView.topAlpha = 1.0;
    //graphView.topColor = [UIColor orangeColor];
    //graphView.topGradient = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations);
    //graphView.bottomAlpha = 1.0;
    //graphView.bottomColor = [UIColor orangeColor];
    view.bottomGradient = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations);
    
}

//自定义X轴 显示标签索引
- (NSArray *)incrementPositionsForXAxisOnLineGraph:(YASimpleGraphView *)graph {
    return @[@0,@1,@2,@3,@4,@5];
}

//Y轴坐标点数
- (NSInteger)numberOfYAxisLabelsOnLineGraph:(YASimpleGraphView *)graph {
    return 5;
}

//自定义popUpView
- (UIView *)popUpViewForLineGraph:(YASimpleGraphView *)graph {
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    label.backgroundColor = [UIColor colorWithRed:146/255.0 green:191/255.0 blue:239/255.0 alpha:1];
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:10];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

//修改相应点位弹出视图
- (void)lineGraph:(YASimpleGraphView *)graph modifyPopupView:(UIView *)popupView forIndex:(NSUInteger)index {
    UILabel *label = (UILabel*)popupView;
    NSString *date = [NSString stringWithFormat:@"%@",self.allDates[index]];
    NSString *str = [NSString stringWithFormat:@" %@ \n %@元 ",date,self.allValues[index]];
    
    CGRect rect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, 40) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} context:nil];
    
    [label setFrame:CGRectMake(0, 0, rect.size.width, rect.size.height)];
    label.textColor = [UIColor whiteColor];
    label.text = str;
    label.hidden = YES;
}



-(QMUILabel *)labOfficial{
    if (!_labOfficial) {
        _labOfficial = [[QMUILabel alloc]initWithFrame:CGRectMake(20, 250+40 , kScreenW, 20)];
        _labOfficial.text = @"531,200";
        _labOfficial.textColor = [UIColor whiteColor];
        _labOfficial.font = [UIFont systemFontOfSize:20];
        [self addSubview:_labOfficial];
    }
    return _labOfficial;
}
-(QMUILabel *)labVessel{
    if (!_labVessel) {
        _labVessel = [[QMUILabel alloc]initWithFrame:CGRectMake(20, 250+80 , kScreenW, 20)];
        _labVessel.text = @"531,200";
        _labVessel.textColor = [UIColor whiteColor];
        _labVessel.font = [UIFont systemFontOfSize:20];
        [self addSubview:_labVessel];
    }
    return _labVessel;
}

-(UIImageView *)imageOfficial{
    if (!_imageOfficial) {
        _imageOfficial = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW-40,  250+40, 20, 20)];
        [self addSubview:_imageOfficial];
    }
    return _imageOfficial;
}

-(UIImageView *)imageVessel{
    if (!_imageVessel) {
        _imageVessel = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW-40,  250+80 , 20, 20)];
        [self addSubview:_imageVessel];
    }
    return _imageVessel;
}
-(QMUILabel *)labSubOfficial{
    if (!_labSubOfficial) {
        _labSubOfficial = [[QMUILabel alloc]initWithFrame:CGRectMake(kScreenW-110, 250+40 , kScreenW, 20)];
        _labSubOfficial.text = @"公价";
        _labSubOfficial.textColor = [UIColor whiteColor];
        _labSubOfficial.font = [UIFont systemFontOfSize:20];
        [self addSubview:_labSubOfficial];
    }
    return _labSubOfficial;
}

-(QMUILabel *)labSubVessel{
    if (!_labSubVessel) {
        _labSubVessel = [[QMUILabel alloc]initWithFrame:CGRectMake(kScreenW-110, 250+80 , kScreenW, 20)];
        _labSubVessel.text = @"二手价";
        _labSubVessel.textColor = [UIColor whiteColor];
        _labSubVessel.font = [UIFont systemFontOfSize:20];
        [self addSubview:_labSubVessel];
    }
    return _labSubVessel;
}




@end
